package com.hqd.jjvm.instructions.change;

import com.hqd.jjvm.instructions.AbstractInstruction;
import com.hqd.jjvm.instructions.InstructionType;
import com.hqd.jjvm.rtda.JThread;

public class TypeChangeInstruction extends AbstractInstruction {

    public TypeChangeInstruction(JThread jThread) {
        super(jThread);
    }

    @Override
    public void execute(InstructionType instructionType) {
        Number number = (Number) popOperandStackVal();
        switch (instructionType) {
            case I2L: {
                Long val = (long) number.intValue();
                pushOperandStackVal(val);
                break;
            }
            case I2F: {
                float val = (float) number.intValue();
                pushOperandStackVal(val);
                break;
            }
            case I2D: {
                double val = number.intValue();
                pushOperandStackVal(val);
                break;
            }
            case L2I: {
                int val = (int) number.longValue();
                pushOperandStackVal(val);
                break;
            }
            case L2F: {
                float val = (float) number.longValue();
                pushOperandStackVal(val);
                break;
            }
            case L2D: {
                double val = (double) number.longValue();
                pushOperandStackVal(val);
                break;
            }
            case F2I: {
                int val = (int) number.floatValue();
                pushOperandStackVal(val);
                break;
            }
            case F2L: {
                long val = (long) number.floatValue();
                pushOperandStackVal(val);
                break;
            }
            case F2D: {
                double val = number.floatValue();
                pushOperandStackVal(val);
                break;
            }
            case D2I: {
                int val = (int) number.doubleValue();
                pushOperandStackVal(val);
                break;
            }
            case D2L: {
                long val = (long) number.doubleValue();
                pushOperandStackVal(val);
                break;
            }
            case D2F: {
                float val = (float) number.doubleValue();
                pushOperandStackVal(val);
                break;
            }
            case I2B: {
                byte val = (byte) number.intValue();
                pushOperandStackVal(val);
                break;
            }
            case I2C: {
                char val = (char) number.intValue();
                pushOperandStackVal(val);
                break;
            }
            case I2S: {
                short val = (short) number.intValue();
                pushOperandStackVal(val);
                break;
            }
        }
    }
}
